package com.chenqi.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenqi.myblog.mapper.BlogMapper;
import com.chenqi.myblog.mapper.TagMapper;
import com.chenqi.myblog.model.Blog;
import com.chenqi.myblog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @program: myblog
 * @author: ChenQi
 * @create: 2021-02-2021/2/2-16-00
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private TagMapper tagMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Blog> findByPage(Integer page,Integer rows) {
        //对每一页的索引进行处理
        int start = (page - 1) * rows;
        List<Blog> blogs = blogMapper.findByPage(start, rows);
        return blogs;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public int findCounts() {
        return blogMapper.selectCount(null);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Blog> findNewBlog(Integer start, Integer rows) {
        return blogMapper.findNewBlog(start,rows);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Blog> findByTypeId(Integer id) {
        return blogMapper.findByTypId(id);
    }

    @Override
    public List<Blog> findAllBlog() {
        return blogMapper.findAllBlog();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
    public void saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setView(0);
        blogMapper.saveBlog(blog);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Blog findBlogById(Integer id) {
        return blogMapper.findBlogById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
    public void saveTags(Integer bid, Integer tid) {
        blogMapper.saveTags(bid,tid);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Blog> searchBlogByTitle(Integer page,Integer rows,String title) {
        //对每一页的索引进行处理
        int start = (page - 1) * rows;
        List<Blog> blogs = blogMapper.searchBlog(start, rows, title);
        return blogs;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Integer searchCount(String title) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("title", title);
        return blogMapper.selectCount(queryWrapper);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
    public void updateView(Integer view,Integer id) {
        blogMapper.updateView(view,id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
    public void deleteBlog(Integer id) {
        blogMapper.deleteById(id);
        //删除博客对应的标签
        tagMapper.deleteTags(id);
    }
}
